﻿/*-----------------------------------------------------------------------------!
! SupplierKazImpl class                                                        !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierKazImpl.cs                                                !
! Function : Implementation of SupplierKaz interface                           !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
! Last changed by: Md. Shaiful Islam, May 01 2011                              !
===============================================================================*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Upadan;
using System.Data.SqlClient;
using Model.Sohaok;
using System.Data;
using Model.Sadharon;

namespace Model.Poribar.SupplierPanjeri
{
    class SupplierKazImpl : ISupplierKaz
    {

        #region SupplierKaz member: getSupplierDetailsById
        /**
         * getSupplierDetailsById
         * @param id
         * @return result object
         **/
        public Result getById(string id)
        {
            Result result = new Result();
            DBSadharon dbUtil = new DBSadharon();
            Util util = new Util();
            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                string query = "Select * from Supplier Where Id='"+ id + "' and Live='L'";
                cmd.CommandText = query;

                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    Supplier supplier = new Supplier();                        
                    while (sdr.Read())
                    {
                        supplier.AccountReference = sdr["AccountReference"].ToString();
                        supplier.CompanyName = sdr["CompanyName"].ToString();
                        supplier.ContactPerson = sdr["ContactPerson"].ToString();
                        supplier.Id = sdr["Id"].ToString();
                        supplier.Live = sdr["Live"].ToString();                        
                    }
                    result.ResultObj = supplier;
                    result.IsSuccess = true;
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get supplier details: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get supplier details: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SupplierKaz member: getSupplierList

        /**
         * getSupplierList get supplier list
         * @return result object
         **/
        public Result getList()
        {
            Result result = new Result();
            DBSadharon dbUtil = new DBSadharon();
            Util util = new Util();            
            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                string query = "Select * from Supplier Where Live='L'";
                cmd.CommandText = query;

                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    List<Supplier> liSupplier = new List<Supplier>();
                    while (sdr.Read())
                    {
                        Supplier supplier = new Supplier();
                        supplier.AccountReference = sdr["AccountReference"].ToString();
                        supplier.CompanyName = sdr["CompanyName"].ToString();
                        supplier.ContactPerson = sdr["ContactPerson"].ToString();
                        supplier.Id = sdr["Id"].ToString();
                        supplier.Live = sdr["Live"].ToString();
                        liSupplier.Add(supplier);
                    }
                    result.ResultObj = liSupplier;
                    result.IsSuccess = true;
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get supplier list: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get supplier list: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SupplierKaz Members: Add Method
        /**
         * add : added new supplier
         * @param object
         * @return Result object
         **/
        public Result add(object _obj)
        {
            Result result = new Result();
            Supplier supplier = new Supplier();
            DBSadharon dbUtil = new DBSadharon();
            Util util = new Util();
            supplier = (Supplier)_obj;

            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                // get UniqId for supplier
                string uniqId = dbUtil.getUniqId(conn, "Supplier", "SP");
                if (uniqId.Equals("falied"))
                {
                    string msg = "Failed to add new supplier: Error in generating uniqeId. If problem persist " +
                                     "please contact with system administrator";
                    result = Util.setSuccessResult(false, msg);
                }
                else
                {
                    supplier.Id = uniqId;
                    supplier.Live = "L";
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.CommandType = CommandType.Text;
                    string query = "Insert into Supplier (Id, AccountReference,CompanyName, ContactPerson, Live) Values(" +
                        "'" + Util.filterString(supplier.Id) + "'," +
                        "'" + Util.filterString(supplier.AccountReference) + "'," +
                        "'" + Util.filterString(supplier.CompanyName) + "'," +
                        "'" + Util.filterString(supplier.ContactPerson) + "'," +
                        "'" + "L" + "'" +
                        ")";
                    cmd.CommandText = query;
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        result = Util.setSuccessResult(true, "New supplier added successfully", supplier);
                    }
                    else
                    {
                        result = Util.setSuccessResult(false, "Failed to add (Query failed)");
                    }
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SupplierKaz Members: Update Method
        /**
         * add : update supplier information
         * @param object
         * @return Result object
         **/
        public Result update(object _obj)
        {
            Result result = new Result();
            Supplier supplier = new Supplier();
            DBSadharon dbUtil = new DBSadharon();
            Util util = new Util();
            supplier = (Supplier)_obj;

            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                string query = "Update Supplier set " +
                    "AccountReference='" + Util.filterString(supplier.AccountReference) + "', " +
                    "CompanyName='" + Util.filterString(supplier.CompanyName) + "', " +
                    "ContactPerson='" + Util.filterString(supplier.ContactPerson) + "' " +
                    "Where Id='" + supplier.Id + "'";
                
                cmd.CommandText = query;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "supplier updated successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to update supplier (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to update supplier: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to update supplier: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SupplierKaz Members: delete Method
        /**
         * add : delete supplier information
         * @param id
         * @return Result object
         **/
        public Result delete(string id)
        {
            Result result = new Result();
            DBSadharon dbUtil = new DBSadharon();
            Util util = new Util();
            
            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                string query = "Update Supplier set Live='D' Where Id='" + id + "'";

                cmd.CommandText = query;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "supplier deleted successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to delete supplier (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete supplier: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete supplier: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion



        
    }
}
